package com.zzyl.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.zzyl.constant.JwtConstants;
import com.zzyl.dto.AccessTokenResultDto;
import com.zzyl.dto.PhoneNumberResultDto;
import com.zzyl.dto.WeChatResultDto;
import com.zzyl.dto.MemberDto;
import com.zzyl.entity.Member;
import com.zzyl.enums.BasicEnum;
import com.zzyl.exception.BaseException;
import com.zzyl.mapper.MemberMapper;
import com.zzyl.properties.JwtProperties;
import com.zzyl.service.CustomerLoginService;
import com.zzyl.service.WeChatService;
import com.zzyl.utils.JwtUtil;
import com.zzyl.vo.LoginVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Objects;

@Slf4j
@Service
public class CustomerLoginServiceImpl implements CustomerLoginService {
    @Autowired
    private WeChatService weChatService;
    @Autowired
    private MemberMapper customerLoginMapper;
    @Autowired
    private JwtProperties jwtProperties;

    @Override
    public LoginVo login(MemberDto memberDto) {
        //先拿到openid
        WeChatResultDto weChatResponseDto = weChatService.getCode2Session(memberDto.getCode());
        if (weChatResponseDto.getErrcode() != null) {
            throw new BaseException(BasicEnum.WX_LOGIN_ERROR);
        }
        String openid = weChatResponseDto.getOpenid();
        log.info("openid:{}", openid);
        //根据openid查库看是否存在，即是否首次登录
        Member member = customerLoginMapper.getCustomerByOpenid(openid);
        if (member != null) {//不是首次登录，之间生产token返回
            return getToken(member);
        }
        //是首次登录，先获取手机号,获取手机号需要先获取access_token
        AccessTokenResultDto accessToken = weChatService.getAccessToken();
        if (accessToken.getErrcode() != null) {
            throw new BaseException(BasicEnum.WX_LOGIN_ERROR);
        }
        log.info("accessToken:{}", openid);
        PhoneNumberResultDto phoneNumber = weChatService.getPhoneNumber(accessToken.getAccess_token(), memberDto.getPhoneCode());
        if(!Objects.equals(phoneNumber.getErrcode(),0)){
            throw new BaseException(BasicEnum.WX_LOGIN_ERROR);
        }
        log.info("phoneNumber:{}", phoneNumber.getPhone_info().getPhoneNumber());

        //向数据库添加数据
        Member member1 = new Member();
        member1.setName(memberDto.getNickName());
        member1.setPhone(phoneNumber.getPhone_info().getPhoneNumber());
        member1.setOpenId(openid);
        member1.setGender(0);
        customerLoginMapper.addCustomer(member1);
        return getToken(member1);
    }

    public LoginVo getToken(Member member) {
        HashMap<String, Object> map = new HashMap<>();
        map.put(JwtConstants.MEMBER_ID, member.getId());
        map.put(JwtConstants.MEMBER_NAME, member.getName());
        String token = JwtUtil.createJWT(jwtProperties.getBase64EncodedSecretKey(), jwtProperties.getTtl(), map);
        LoginVo loginVo = BeanUtil.copyProperties(member, LoginVo.class);
        loginVo.setToken(token);
        loginVo.setNickName(member.getName());
        return loginVo;
    }
}
